package com.example.fan.airline_reservation;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.fan.airline_reservation.Adapter.ticketlist_adapter;
import com.example.fan.airline_reservation.Model.Fight;
import com.example.fan.airline_reservation.Model.TicketList;
import com.example.fan.airline_reservation.Model.USER;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;

public class traveler_ticketlist extends AppCompatActivity {
    List<TicketList> ticketlist=new ArrayList<TicketList>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_traveler_ticketlist);

        final Intent intent=getIntent();
        String FROM=intent.getStringExtra("from");
        String TO=intent.getStringExtra("to");
        Date Ldate = new Date(intent.getLongExtra("date",0));
        Date Hdate=new Date(Ldate.getYear(),Ldate.getMonth(),Ldate.getDate(),23,59,59);
        final USER user= (USER) intent.getSerializableExtra("USER11");
        Long LDATE=Ldate.getTime();
        Long HDATE=Hdate.getTime();
        Log.e("aaaaaaaaaaaaaaaa",Hdate.toString());

        final ListView listView=(ListView)findViewById(R.id.ticketlist);
        String bq ="select * from Fight where FROM = '" + FROM + "'"+"and TO= '"
                + TO + "'"+"and DATE>=" +LDATE+" and DATE<="+HDATE+"";
        /*String bq ="select * from USER ";*/
        /*Log.d("test",bq);*/
        new BmobQuery<Fight>().doSQLQuery(bq, new SQLQueryListener<Fight>() {
            @Override
            public void done(BmobQueryResult<Fight> bmobQueryResult, BmobException e) {
                Log.e("tra ticket list","public void done");
                List<Fight> list = (List<Fight>) bmobQueryResult.getResults();
                initticket(list);
                ticketlist_adapter ticketlist_adapter=new ticketlist_adapter(traveler_ticketlist.this,
                        R.layout.ticketlist_item,ticketlist);
                listView.setAdapter(ticketlist_adapter);
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TicketList ticket=ticketlist.get(position);
                Intent openBuyIntf=new Intent(traveler_ticketlist.this,traveler_bookticket.class);
                openBuyIntf.putExtra("splace",ticket.getStartplace());
                openBuyIntf.putExtra("aplace",ticket.getArriveplace());
                openBuyIntf.putExtra("flightNUM",ticket.getFlightNO());
                openBuyIntf.putExtra("flytime",ticket.getFlytime());
                openBuyIntf.putExtra("price",ticket.getPrice());
                openBuyIntf.putExtra("stime",ticket.getStarttime());
                openBuyIntf.putExtra("atime",ticket.getArrivetime());
                openBuyIntf.putExtra("sdate",ticket.getStartdate());
                openBuyIntf.putExtra("FromCity",ticket.getFromcity());
                openBuyIntf.putExtra("ToCity",ticket.getTocity());
                openBuyIntf.putExtra("planeModel",ticket.getPlanemodel());
                openBuyIntf.putExtra("objectid_fight",ticket.getObjectid());
                openBuyIntf.putExtra("time",ticket.getTime());
                openBuyIntf.putExtra("USER11",user);
                startActivity(openBuyIntf);
            }
        });
    }
    public void ToastShow(String message){
        Toast.makeText(traveler_ticketlist.this,message,Toast.LENGTH_SHORT).show();
    }
    private void initticket(List<Fight> fightlist){

        for(Fight fight:fightlist){
            if(fight.getCurrentState()<2){
                String price=fight.getPRICE()+"";
                String planemodel=fight.getAIRCRAFT_MODEL();
                String FAirport=fight.getFROMNAME();
                String TAirport=fight.getTONAME();
                String startplace=fight.getFROM()+FAirport;
                String arriveplace=fight.getTO()+TAirport;
                Date D_stime=fight.getDATE();
                Date D_atime=fight.getTODATE();
                String starttime=getTime(D_stime);
                String arrivetime=getTime(D_atime);
                String flytime=getFlytime(D_stime,D_atime);
                String flihtNO=fight.getFLIGHT();
                String objectid=fight.getObjectId();
                Long time=fight.getDATE().getTime();

                TicketList ticket=new TicketList(starttime,startplace,arrivetime
                        ,arriveplace,price,flytime,planemodel,flihtNO);
                ticket.setStartdate(getMidTime(D_stime));
                ticket.setArrivedate(getMidTime(D_atime));
                ticket.setFromcity(fight.getFROM());
                ticket.setTocity(fight.getTO());
                ticket.setObjectid(objectid);
                ticket.setTime(time);
                Log.e("startplace:",ticket.getPlanemodel());
                ticketlist.add(ticket);
            }
        }
    }
    private String getTime(Date source){
        String date=new String();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(source);
        int Hour=calendar.get(Calendar.HOUR_OF_DAY);
        int Minute=calendar.get(Calendar.MINUTE);
        date=Hour+":"+Minute;
        return date;
    }
    private String getMidTime(Date source){
        String date=new String();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(source);
        int Year=calendar.get(Calendar.YEAR);
        int Month=calendar.get(Calendar.MONTH)+1;
        int Day=calendar.get(Calendar.DAY_OF_MONTH);
        date=Year+"年"+Month+"月"+Day+"日";
        return date;
    }
    private String getMoreTime(Date source) {
        String date=new String();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(source);
        int Hour=calendar.get(Calendar.HOUR_OF_DAY);
        int Minute=calendar.get(Calendar.MINUTE);
        int Second=calendar.get(Calendar.SECOND);
        int Year=calendar.get(Calendar.YEAR);
        int Month=calendar.get(Calendar.MONTH)+1;
        int Day=calendar.get(Calendar.DAY_OF_MONTH);
        date=Year+"-"+Month+"-"+Day+" "+Hour+":"+Minute+":"+Second;
        return date;
    }
    private String getFlytime(Date source1,Date source2){
        String Flytime=new String();
        String sou1=getMoreTime(source1);
        String sou2=getMoreTime(source2);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try
        {
            Date d1 = df.parse(sou1);
            Date d2 = df.parse(sou2);
            long diff = d2.getTime() - d1.getTime();//这样得到的差值是微秒级别
            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
            long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
            Flytime=days+"天"+hours+"小时"+minutes+"分";
        }catch (Exception e)
        {
        }
        return Flytime;
    }
}
